public class Leetcode47 {
    public static void main(String[] args) {
//        System.out.println(maxValue(new int[][]{{1, 3, 1}, {1, 5, 1}, {4, 2, 1}}));
        System.out.println(maxValue(new int[][]{
                {7, 1, 3, 5, 8, 9, 9, 2, 1, 9, 0, 8, 3, 1, 6, 6, 9, 5},
                {9, 5, 9, 4, 0, 4, 8, 8, 9, 5, 7, 3, 6, 6, 6, 9, 1, 6},
                {8, 2, 9, 1, 3, 1, 9, 7, 2, 5, 3, 1, 2, 4, 8, 2, 8, 8},
                {6, 7, 9, 8, 4, 8, 3, 0, 4, 0, 9, 6, 6, 0, 0, 5, 1, 4},
                {7, 1, 3, 1, 8, 8, 3, 1, 2, 1, 5, 0, 2, 1, 9, 1, 1, 4},
                {9, 5, 4, 3, 5, 6, 1, 3, 6, 4, 9, 7, 0, 8, 0, 3, 9, 9},
                {1, 4, 2, 5, 8, 7, 7, 0, 0, 7, 1, 2, 1, 2, 7, 7, 7, 4},
                {3, 9, 7, 9, 5, 8, 9, 5, 6, 9, 8, 8, 0, 1, 4, 2, 8, 2},
                {1, 5, 2, 2, 2, 5, 6, 3, 9, 3, 1, 7, 9, 6, 8, 6, 8, 3},
                {5, 7, 8, 3, 8, 8, 3, 9, 9, 8, 1, 9, 2, 5, 4, 7, 7, 7},
                {2, 3, 2, 4, 8, 5, 1, 7, 2, 9, 5, 2, 4, 2, 9, 2, 8, 7},
                {0, 1, 6, 1, 1, 0, 0, 6, 5, 4, 3, 4, 3, 7, 9, 6, 1, 9}
        }));

        System.out.println(maxValue(new int[][]{
                {3, 8, 6, 0, 5, 9, 9, 6, 3, 4, 0, 5, 7, 3, 9, 3},
                {0, 9, 2, 5, 5, 4, 9, 1, 4, 6, 9, 5, 6, 7, 3, 2},
                {8, 2, 2, 3, 3, 3, 1, 6, 9, 1, 1, 6, 6, 2, 1, 9},
                {1, 3, 6, 9, 9, 5, 0, 3, 4, 9, 1, 0, 9, 6, 2, 7},
                {8, 6, 2, 2, 1, 3, 0, 0, 7, 2, 7, 5, 4, 8, 4, 8},
                {4, 1, 9, 5, 8, 9, 9, 2, 0, 2, 5, 1, 8, 7, 0, 9},
                {6, 2, 1, 7, 8, 1, 8, 5, 5, 7, 0, 2, 5, 7, 2, 1},
                {8, 1, 7, 6, 2, 8, 1, 2, 2, 6, 4, 0, 5, 4, 1, 3},
                {9, 2, 1, 7, 6, 1, 4, 3, 8, 6, 5, 5, 3, 9, 7, 3},
                {0, 6, 0, 2, 4, 3, 7, 6, 1, 3, 8, 6, 9, 0, 0, 8},
                {4, 3, 7, 2, 4, 3, 6, 4, 0, 3, 9, 5, 3, 6, 9, 3},
                {2, 1, 8, 8, 4, 5, 6, 5, 8, 7, 3, 7, 7, 5, 8, 3},
                {0, 7, 6, 6, 1, 2, 0, 3, 5, 0, 8, 0, 8, 7, 4, 3},
                {0, 4, 3, 4, 9, 0, 1, 9, 7, 7, 8, 6, 4, 6, 9, 5},
                {6, 5, 1, 9, 9, 2, 2, 7, 4, 2, 7, 2, 2, 3, 7, 2},
                {7, 1, 9, 6, 1, 2, 7, 0, 9, 6, 6, 4, 4, 5, 1, 0},
                {3, 4, 9, 2, 8, 3, 1, 2, 6, 9, 7, 0, 2, 4, 2, 0},
                {5, 1, 8, 8, 4, 6, 8, 5, 2, 4, 1, 6, 2, 2, 9, 7}
        }));
    }

//    public static int maxValue(int[][] grid) {
//        int column = grid.length;
//        int row = grid[0].length;
//        Queue<int[]> queue = new LinkedList<>();
//        queue.offer(new int[]{0, 0});
//
//
//        int[][] isSuccess = new int[column][row];
//
//        int[][] res = new int[column][row];
//        res[0][0] = grid[0][0];
//        while (!queue.isEmpty()) {
//            int[] index = queue.poll();
//            int x = index[1];
//            int y = index[0];
//            if (isSuccess[y][x] == 2) {
//                continue;
//            }
//
//            isSuccess[y][x]++;
//
//            if (x + 1 < row) {
//                res[y][x + 1] = Math.max(res[y][x + 1], res[y][x] + grid[y][x + 1]);
//                queue.offer(new int[]{y, x + 1});
//            }
//            if (y + 1 < column) {
//                res[y + 1][x] = Math.max(res[y + 1][x], res[y][x] + grid[y + 1][x]);
//                queue.offer(new int[]{y + 1, x});
//            }
//        }
//
//        return res[column - 1][row - 1];
//    }

    public static int maxValue(int[][] grid) {
        int[][] res = new int[grid.length + 1][grid[0].length + 1];
        res[0][0] = grid[0][0];
        return maxValue(0, 0, grid, res);
    }

    public static int maxValue(int x, int y, int[][] grid, int[][] res) {
        if (x >= grid.length || y >= grid[0].length) {
            return 0;
        }

        int rightMax;
        if (res[x + 1][y] != 0) {
            rightMax = res[x + 1][y];
        } else {
            rightMax = maxValue(x + 1, y, grid, res);
        }

        int downMax;
        if (res[x][y + 1] != 0) {
            downMax = res[x][y + 1];
        } else {
            downMax = maxValue(x, y + 1, grid, res);
        }

        res[x][y] = Math.max(rightMax, downMax) + grid[x][y];

        return res[x][y];
    }

}
